package com.example.paper.fragment;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.example.db.DBManager;
import com.example.db.Goods;
import com.example.mvp.model.BaseEntity;
import com.example.mvp.view.BaseFragment;
import com.example.paper.R;
import com.example.paper.brean.UserIteamEntivy;
import com.example.paper.brean.UserMenuEntivy;
import com.example.paper.loginreg.ChangeActivity;
import com.example.paper.loginreg.LoginActivity;
import com.example.paper.mvp.contract.IUserContract;
import com.example.paper.mvp.model.UserModel;
import com.example.paper.mvp.presenter.UserPresenter;
import com.example.paper.mvp.view.user.adapter.UserAdapter;
import com.example.paper.mvp.view.user.adapter.UserMenuAdapter;

import java.util.List;


public class MeFragment extends BaseFragment<UserPresenter> implements IUserContract.IUserView {

    private View menuView, timeLineView;
    private RecyclerView menu_rv;
    private RecyclerView user_rv;
    private UserAdapter adapter;
    private UserMenuAdapter menuAdapter;
    private TextView user_name;
    private List<Goods> goods;
    private getUserBroadcast getUserBroadcast;
    private ImageView menu_img;

    @Override
    public int bindLayout() {
        return R.layout.fragment_user;
    }

    @Override
    public void initView() {

        user_rv = (RecyclerView) rootView.findViewById(R.id.user_rv);
        menuAdapter = new UserMenuAdapter();
        adapter = new UserAdapter();

        menuView = LayoutInflater.from(getContext()).inflate(R.layout.view_user_menu, null);
        //获取id
        user_name = menuView.findViewById(R.id.user_name);
        menu_img = menuView.findViewById(R.id.menu_img);

        timeLineView = LayoutInflater.from(getContext()).inflate(R.layout.view_user_speed, null);

        menu_rv = (RecyclerView) menuView.findViewById(R.id.menu_rv);


        //添加线
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        user_rv.addItemDecoration(dividerItemDecoration);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        user_rv.setLayoutManager(manager);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);


        menu_rv.setLayoutManager(gridLayoutManager);
        menu_rv.setAdapter(menuAdapter);

        //适配器
        //添加头布局
        adapter.addHeaderView(menuView);
        adapter.addHeaderView(timeLineView);
        user_rv.setAdapter(adapter);




    }


    @Override
    public void initData() {
        mPresenter = new UserPresenter(new UserModel(), this);
        mPresenter.initData();

        //注册广播
        getUserBroadcast = new getUserBroadcast();
        IntentFilter intentFilter = new IntentFilter();
        //注册地址值
        intentFilter.addAction("sends_user_name");
        getContext().registerReceiver(getUserBroadcast, intentFilter);

        //跳转
        user_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                goods = DBManager.getDbManager().getDaoSession().loadAll(Goods.class);
                if (goods.size() == 0) {
                    //登陆页面
                    Intent intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    for (Goods good : goods) {
                        if (good.getLogin()) {
                            //为ture已经登陆 去修改页面
                            Intent intent = new Intent(getContext(), ChangeActivity.class);
                            startActivity(intent);
                        } else {
                            //登陆页面
                            Intent intent = new Intent(getContext(), LoginActivity.class);
                            startActivity(intent);
                        }

                    }
                }

                //查询所有
                LogUtils.json(goods);

            }
        });

        //赋值名字
        goods = DBManager.getDbManager().getDaoSession().loadAll(Goods.class);
        for (Goods good : goods) {
            LogUtils.json(good);
            //赋值给名字
            MeFragment.this.user_name.setText(""+good.getUser_name());
            if (!good.getUser_img().equals("")){
                //头像加载
                Glide.with(getContext()).load(good.getUser_img()).circleCrop().into(menu_img);
            }
        }

    }


    //广播接收者
    class getUserBroadcast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //判断地址值是否相等
            if (intent.getAction().equals("sends_user_name")) {
                //接受传过来的值
                String send_userName = intent.getStringExtra("send_userName");
                //赋值
                user_name.setText("" + send_userName);
            }
        }

    }

    @Override
    public void onDestroy() {
        //解除广播
        getContext().unregisterReceiver(getUserBroadcast);
        super.onDestroy();
    }

    @Override
    public void refresh(BaseEntity entity) {

    }

    @Override
    public void refreshMenu(List<UserMenuEntivy> menues) {
        menuAdapter.setNewInstance(menues);
    }

    @Override
    public void refreshItemes(List<UserIteamEntivy> itemes) {
        adapter.setNewInstance(itemes);
    }


}